package com.ruoyi.system.service.impl;

import java.util.List;

import cn.wildfirechat.common.ErrorCode;
import cn.wildfirechat.pojos.InputCreateChannel;
import cn.wildfirechat.pojos.OutputCreateChannel;
import cn.wildfirechat.sdk.ChannelAdmin;
import cn.wildfirechat.sdk.ChannelServiceApi;
import cn.wildfirechat.sdk.model.IMResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TChannelMapper;
import com.ruoyi.system.domain.TChannel;
import com.ruoyi.system.service.ITChannelService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-18
 */
@Service
public class TChannelServiceImpl implements ITChannelService 
{
    @Autowired
    private TChannelMapper tChannelMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TChannel selectTChannelById(Long id)
    {
        return tChannelMapper.selectTChannelById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param tChannel 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TChannel> selectTChannelList(TChannel tChannel)
    {
        return tChannelMapper.selectTChannelList(tChannel);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param tChannel 【请填写功能名称】
     * @return 结果
     */
    @Override
    public Boolean insertTChannel(TChannel tChannel)
    {
        InputCreateChannel inputCreateChannel = new InputCreateChannel();
        BeanUtils.copyProperties(tChannel,inputCreateChannel);
        inputCreateChannel.setAuto(tChannel.getAutomatic());
        IMResult<OutputCreateChannel> resultCreateChannel = null;
        try {
            resultCreateChannel = ChannelAdmin.createChannel(inputCreateChannel);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (resultCreateChannel != null && resultCreateChannel.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
            System.out.println("success");
            inputCreateChannel.setTargetId(resultCreateChannel.result.getTargetId());
        } else {
            System.out.println("create channel failure");
        }
        return true;
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param tChannel 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTChannel(TChannel tChannel)
    {
        return tChannelMapper.updateTChannel(tChannel);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTChannelByIds(Long[] ids)
    {
        return tChannelMapper.deleteTChannelByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTChannelById(Long id)
    {
        try {
            ChannelAdmin.destroyChannel(id.toString());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return 1;
    }

    @Override
    public Integer getCount() {
        return tChannelMapper.getCount();
    }
}
